package supercafe.more;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class XMLHandler {
	
	
	
	public static String getCharacterDataFromElement(Element e) {
	    Node child = e.getFirstChild();
	    if (child instanceof CharacterData) {
	      CharacterData cd = (CharacterData) child;
	      return cd.getData();
	    }
	    return "";
	  }
	
	public static Hashtable<String, String> getLoginResponse(String xmlResponse){
		Hashtable<String, String> result = new Hashtable<String, String>();
		try{
			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xmlResponse));
			Document doc = db.parse(is);
			Element statusN = (Element)doc.getElementsByTagName("status").item(0);
			String status = getCharacterDataFromElement(statusN);
			result.put("status", status);
			// nếu thành công thì lấy về session id
			if(status.equals("1")){
				Element employeeidN = (Element)doc.getElementsByTagName("employee_id").item(0);
				String employeeid = getCharacterDataFromElement(employeeidN);
				result.put("employeeid", employeeid);

				Element employeenamedN = (Element)doc.getElementsByTagName("employee_name").item(0);
				String employeename = getCharacterDataFromElement(employeenamedN);
				result.put("employeename", employeename);
				
				Element sessionidN = (Element)doc.getElementsByTagName("session_id").item(0);
				String sessionid = getCharacterDataFromElement(sessionidN);
				result.put("sessionid", sessionid);
			}
			// nếu có lỗi thì lấy message
			else{
				Element messageN = (Element)doc.getElementsByTagName("message").item(0);
				String message = getCharacterDataFromElement(statusN);
				result.put("message", message);
			}
		}catch(Exception e){
			e.printStackTrace();			
		}
		return result;
	}
	
	public static Vector<Area> getAreasWithTable(String xmlResponse){
		Vector<Area> areaswithtables = new Vector<Area>();
		try{
			DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xmlResponse));
			Document doc = db.parse(is);
			NodeList areas = doc.getElementsByTagName("area");
			int numOfAreas = areas.getLength();
			for(int i=0;i<numOfAreas;i++){
				Element currentArea = (Element)areas.item(i);
				int areaId = Integer.parseInt(currentArea.getAttribute("id"));
				String areaName = currentArea.getAttribute("name");
				Area areaTemp = new Area(areaId, areaName);
				NodeList tables = currentArea.getElementsByTagName("table");
				int numOfTablesInArea = tables.getLength();
				for(int j=0;j<numOfTablesInArea;j++){
					Element currentTable = (Element)tables.item(j);
					int tableId = Integer.parseInt(currentTable.getAttribute("id"));
					String tableName = getCharacterDataFromElement((Element)currentTable.getElementsByTagName("name").item(0));
					boolean used = Integer.parseInt(getCharacterDataFromElement((Element)currentTable.getElementsByTagName("used").item(0)))==1?true:false;
					boolean ordered = Integer.parseInt(getCharacterDataFromElement((Element)currentTable.getElementsByTagName("ordered").item(0)))==1?true:false;
					boolean prepared = Integer.parseInt(getCharacterDataFromElement((Element)currentTable.getElementsByTagName("prepared").item(0)))==1?true:false;
					Table tempTable = new Table(tableId, tableName, used,ordered,prepared);
					areaTemp.addTable(tempTable);
				}
				areaswithtables.add(areaTemp);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return areaswithtables;
	}
}
